<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once(sfConfig::get('sf_plugins_dir') . '/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 23319 2009-10-25 12:22:23Z Kris.Wallsmith $
 */
class sfGuardAuthActions extends BasesfGuardAuthActions {

    public function executeSignin($request) {
        $this->mode = $mode = $this->getRequestParameter('mode');

        if ($request->hasParameter('mode'))
            $this->setTemplate('trainerLogin');


        $user = $this->getUser();
        if ($user->isAuthenticated()) {
            return $this->redirect('@homepage');
        }

        $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
        $this->form = new $class();

        if ($request->isMethod('post')) {
            $this->form->bind($request->getParameter('signin'));
            if ($this->form->isValid()) {
                $values = $this->form->getValues();




                $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);

                // always redirect to a URL set in app.yml
                // or to the referer
                // or to the homepage

                sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));
                if ($request->hasParameter('mode')) {
                    $mode = $request->getParameter('mode');
                    if ($mode == 'teacher') {
                        $signinUrl = url_for('home/teacher');
                        $this->getUser()->setAttribute('teacher_mode', true);
                    } else {
                        $signinUrl = url_for('assignment/index');
                    }
                } else {
                    $signinUrl = sfConfig::get('app_sf_guard_plugin_success_signin_url', $user->getReferer($request->getReferer()));
                }

                if ($request->isXmlHttpRequest()) {
                    return $this->renderText('<script type="text/javascript">window.location="' . $signinUrl . '"</script>');
                }
                return $this->redirect('' != $signinUrl ? $signinUrl : '@homepage');
            } else {
                if ($request->isXmlHttpRequest()) {
                    return $this->renderPartial('signin_form');
                }
            }
        } else {
            if ($request->isXmlHttpRequest()) {
                //$this->getResponse()->setHeaderOnly(true);
                //$this->getResponse()->setStatusCode(401);
                //return sfView::NONE;
                return $this->renderPartial('signin_form');
            }

            // if we have been forwarded, then the referer is the current URL
            // if not, this is the referer of the current request
            $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

            $module = sfConfig::get('sf_login_module');
            if ($this->getModuleName() != $module) {
                return $this->redirect($module . '/' . sfConfig::get('sf_login_action'));
            }

            $this->getResponse()->setStatusCode(401);
        }

        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('signin_form');
        }
    }

    public function executeChange_password($request) {
        $this->form = new sfGuardChangeUserPasswordForm($this->getUser()->getGuardUser());

        if ($request->isMethod('POST')) {
            $this->form->bind($request->getParameter($this->form->getName()));

            if ($this->form->isValid()) {
                $this->form->save();
                $this->message = 'password changed successfully';
            } else {
            }
        }
    }

}
